package com.jlnk.androidfmc.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.jlnk.androidfmc.R;
import com.jlnk.androidfmc.entity.Address;

import java.util.ArrayList;
import java.util.List;

public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.ItemViewHolder> {

  private final OnClickListener onClick;
  private List<Address> items = new ArrayList<>();

  public interface OnClickListener {

    void onClick(Address address);
  }

  public AddressAdapter(OnClickListener onClick) {
    this.onClick = onClick;
  }

  public static class ItemViewHolder extends RecyclerView.ViewHolder {

    private TextView tvInitials, tvName, tvPhone, tvAddress;
    private CardView containerIsDefault;

    public ItemViewHolder(@NonNull View itemView) {
      super(itemView);
      tvInitials = itemView.findViewById(R.id.tv_initials);
      tvName = itemView.findViewById(R.id.tv_name);
      tvPhone = itemView.findViewById(R.id.tv_phone);
      tvAddress = itemView.findViewById(R.id.tv_address);
      containerIsDefault = itemView.findViewById(R.id.container_is_default);
    }
  }

  @NonNull
  @Override
  public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    return new ItemViewHolder(
        LayoutInflater.from(parent.getContext()).inflate(R.layout.item_address, parent, false));
  }

  @Override
  public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
    final Address item = items.get(position);
    String initials = "";
    if (!item.getName().isEmpty()) {
      initials = item.getName().substring(0, 1);
    }
    holder.tvInitials.setText(initials);
    holder.tvName.setText(item.getName());
    holder.tvPhone.setText(item.getPhone());
    holder.tvAddress.setText(
        item.getProvince() + " " + item.getCity() + " " + item.getDistrict() + " "
            + item.getAddressDetail());
    if (item.getIsDefault() == 0) {
      holder.containerIsDefault.setVisibility(View.GONE);
    } else {
      holder.containerIsDefault.setVisibility(View.VISIBLE);
    }

    holder.itemView.setOnClickListener(v -> onClick.onClick(items.get(position)));
  }

  @Override
  public int getItemCount() {
    return items.size();
  }

  public void insertItem(Address item) {
    if (item.getIsDefault() == 1) {
      clearAllIsDefault();
      items.add(0, item);
      notifyItemInserted(0);
    } else {
      items.add(item);
      notifyItemInserted(items.size() - 1);
    }
    sortItems();
    notifyDataSetChanged();
  }

  public void insertAllItems(List<Address> items) {
    this.items.clear();
    this.items.addAll(items);
    sortItems();
    notifyDataSetChanged();
  }

  public void updateItem(Address item) {
    for (int i = 0; i < items.size(); i++) {
      if (items.get(i).getId() == item.getId()) {
        if (item.getIsDefault() == 1) {
          clearAllIsDefault();
          items.remove(i);
          items.add(0, item);
        } else {
          items.set(i, item);
        }
        sortItems();
        notifyDataSetChanged();
        return;
      }
    }
  }

  public void delItem(int id) {
    for (int i = 0; i < items.size(); i++) {
      if (items.get(i).getId() == id) {
        items.remove(i);
        notifyItemRemoved(i);
        return;
      }
    }
  }

  private void clearAllIsDefault() {
    for (Address address : items) {
      address.setIsDefault(0);
    }
  }

  private void sortItems() {
    items.sort((a, b) -> b.getIsDefault() - a.getIsDefault());
  }
}
